﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FastShell.Common.Model;

namespace FastShell
{
    /// <summary>
    /// ConnectInfo.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectDialog : Window
    {


        public delegate void SentValue(HostConfig value);  //声明委托
        public SentValue sentValue;
        private HostConfig Config;

        public ConnectDialog()
        {
            InitializeComponent();

        }
      

        public ConnectDialog(HostConfig config) : this()
        {
            this.Config = config;

            this.name.Text = config.Name;
            this.host.Text = config.Host;
            this.port.Text = config.Port.ToString();
            this.userName.Text = config.UserName;
            this.password.Password = config.Password;
            if (config.Type == null)
            {
                this.Config.Type = "linux";
                this.Config.Port = 22;
                this.port.Text = "22";
                this.linuxRadioBtn.IsChecked = true;
            }
        }
       
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void typeRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            var radioButton = sender as RadioButton;
            if (radioButton == null)
                return;
            var val = radioButton.Content.ToString();
            if (val.Equals("目录"))
            {
                this.Config.Type = "folder";
                this.host.IsEnabled = false;
                this.host.Text = "";
                this.port.IsEnabled = false;
                this.port.Text = "";
                this.userName.IsEnabled = false;
                this.userName.Text = "";
                this.password.IsEnabled = false;
                this.password.Password = ""; 
            }
            else
            {
                this.host.IsEnabled = true;
                this.host.Text = this.Config.Host;
                this.port.IsEnabled = true;
                this.port.Text = this.Config.Port.ToString();
                this.userName.IsEnabled = true;
                this.userName.Text = this.Config.UserName ;
                this.password.IsEnabled = true;
                this.password.Password = this.Config.Password;

                if(val.Equals("linux主机"))
                {
                    this.Config.Type = "linux";
                }
                else if (val.Equals("win主机"))
                {
                    this.Config.Type = "windows";
                }
            }
            
        }
        private void connectBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.name.Text == "")
            {
                MessageBox.Show("名称不能为空","提示",MessageBoxButton.OK,MessageBoxImage.Error);
                return;
            }
            this.Config.Name = this.name.Text;
            this.Config.Host = this.host.Text;
            this.Config.Port = int.Parse(this.port.Text);
            this.Config.UserName = this.userName.Text;
            if (this.password.Password != null)
            {
                this.Config.Password = this.password.Password;
            }
            sentValue(this.Config);
            this.Close();
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
